package com.order.ctl;

import com.order.service.OrderService;
import com.payment.client.PaymentClient;
import com.payment.client.request.PaymentRequest;
import com.payment.client.response.PaymentResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * dongzhihua
 * 2022/10/20 18:17
 */
@Slf4j
@RestController
@RequestMapping("order")
public class OrderCtl {

    @Value("${server.port:8080}")
    private String port;

    @Resource(name = "order-service-feign")
    private OrderService orderService;

    @RequestMapping("pay")
    public String pay(Integer orderId) {
        String payResult = orderService.pay(orderId);
        log.info("pay result={}", payResult);
        return String.format("order pay success orderId=%s port=%s \n\t result=%s", orderId, port, payResult);
    }

    @RequestMapping("get")
    public String get(Integer orderId) {
        PaymentResponse payResult = orderService.get(orderId);
        log.info("pay result={}", payResult);
        return String.format("order pay success orderId=%s port=%s \n\t result=%s", orderId, port, payResult);
    }

    @Resource
    private PaymentClient paymentClient;

    @RequestMapping("timeout")
    public String timeout(Integer orderId) {
        final PaymentRequest request = new PaymentRequest();
        request.setOrderNo(orderId + "");
        final PaymentResponse response = paymentClient.timeout(request);
        return String.format("order pay success orderId=%s port=%s \n\t result=%s", orderId, port, response);
    }
}
